package com.chianghao.rock.system.service.role_scope;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chianghao.core.frame.service.BaseService;
import com.chianghao.core.mybatis.sql.SqlWrapper;
import com.chianghao.core.mybatis.sql.db_enum.SqlOperators;
import com.chianghao.rock.system.dao.role_scope.RoleScopeDao;
import com.chianghao.rock.system.dao.role_scope.RoleScopeMapper;
import com.chianghao.rock.system.dao.route.RouteDao;
import com.chianghao.rock.system.dao.route.RouteMapper;
import com.chianghao.rock.system.entity.RoleScope;
import com.chianghao.rock.system.entity.Route;

@Service
public class RoleScopeService extends BaseService<RoleScopeMapper, RoleScope> {

	@Autowired
	RouteMapper routeMapper;
	
	@Autowired
	RouteDao routeDao;
	
	@Autowired
	RoleScopeDao dao;
	
	public void doBuildRoleRoute(Long roleId, List<Long> routeIds,Integer scopeType) {
		RoleScope where = new  RoleScope();
		where.setRoleId(roleId);
		this.mapper.deleteByEntity(where);
		List<RoleScope> list  = new ArrayList<RoleScope>();
		for(Long id:routeIds) {
			RoleScope item  = new RoleScope();
			item.setRoleId(roleId);
			item.setScopeId(id);
			item.setScopeType(scopeType);
			list.add(item);
		}
		this.mapper.batchInsert(list);
	}

	
	public List<Route> queryRouteList(Long roleId, Integer type) {
		RoleScope where  = new RoleScope();
		where.setRoleId(roleId);
		where.setScopeType(type);
		List<RoleScope> list  = this.mapper.queryListByEntity(where,null);
		List<Long> ids = list.stream().map(item->item.getScopeId()).collect(Collectors.toList());
		Long[] array  = ids.toArray(new Long[ids.size()]);
		return this.routeMapper.queryList(new SqlWrapper().addCondition(Route::getId,SqlOperators.in,array));
	}

	
	public List<Route> queryByRoleIds(List<Long> rouleIds) {
		List<RoleScope> list  = this.dao.queryByRoleIds(rouleIds,1);
		List<Long> routeIds = list.stream().map(item->item.getScopeId()).collect(Collectors.toList());
		return routeDao.queryListById(routeIds);  
	}
	

}